package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.lib.common.base.BaseActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.BaseBean;
import tq.com.tqstudent.bean.ClassCardBean;
import tq.com.tqstudent.bean.event.ClassActivateEvent;
import tq.com.tqstudent.net.ok.DialogCallback;
import tq.com.tqstudent.net.ok.StringCallback;

/**
 * 班卡激活
 *
 * @Author rames
 * @date 2019/2/28 22:55
 */
public class ClassActivateActivity extends BaseActivity {
    @BindView(R.id.verify_edit)
    EditText verifyEdit;
    private AlertDialog.Builder builder;

    public static void show(Activity activity) {
        Intent intent = new Intent(activity, ClassActivateActivity.class);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    public int initLayout() {
        return R.layout.activity_class_activate;
    }

    @Override
    protected void setLayoutViewBefore() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("加入班级");
    }

    @OnClick(R.id.enter_tv)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.enter_tv:
                if (!TextUtils.isEmpty(verifyEdit.getText().toString().trim())) {
                    getData();
                } else {
                    showToast("请输入激活码");
                }
                break;
        }
    }

    private void showSimpleDialog() {
        builder = new AlertDialog.Builder(this);
//        builder.setTitle("");
        builder.setMessage("你的班卡已激活！");

        //监听下方button点击事件
        builder.setPositiveButton("好的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                dialog.dismiss();
                ClassActivateActivity.this.finish();
            }
        });

        //设置对话框是可取消的
        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    @Override
    public void initData() {
    }

    private void getData() {
        String uri = "api/user/actcard";
        String json = "{\"card_id\":\"" + verifyEdit.getText().toString() + "\"}";

        OkGo.<BaseBean<Object>>post(App.getHttpUrl(uri))
                .tag(this)
                .upJson(json)
                .execute(new DialogCallback<Object>(this) {
                    @Override
                    public void handleSuccess(Object data) {
                        showSimpleDialog();
                    }
                });
    }
}
